<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;
class PropController extends CommonController
{
    public function index()
    {
        $game_id = M('game')->select();
        $currency_id = M('currency')->select();
        $this->assign("game_id",$game_id);
        $this->assign("currency_id",$currency_id);
        $this->display();
    }

    /**
     *道具列表
     */
    public function propList(){
        $where = array();
        $sort = I('request.sort', 'orderid');
        $order = I('request.order', 'asc');
        $search = I('search');
        if(!empty($search)) $where['i.title|g.game|c.currency'] = array('like','%'.$search.'%');

        $result = M('item as i')
                ->join('LEFT JOIN '.C('DB_PREFIX').'game as g ON i.gameid = g.gameid')
                ->join('LEFT JOIN '.C('DB_PREFIX').'currency as c ON i.currencyid = c.curyid')
                ->field('i.*,g.game,c.currency')
                ->where($where)
                ->order($sort." ".$order)
                ->select();       
        foreach ($result as $k => &$v) {
            $v['orderid']=(int)$v['orderid'];
        }
        $this->ajaxReturn($result);
    }

    /**
     * 货币列表
     */

    public function gameList(){
        $res = M('game')->select();
        $this->ajaxReturn($res);
    }

    public function currencyList2(){
        $gameid = $_GET['gameid'];
        $list = M('currency')->where("gameid='$gameid' and is_delete='1'")->select();
        $this->ajaxReturn($list);
    }


    /**
    * 
    * @author wang
    * @param 
    */
    public function cury(){
        $listObj = M('currency');
        $gameid = I('gameid');
        $curylist = $listObj->where("gameid='$gameid'")->select();
        $data=array('status'=>0,'cury'=>$curylist);
        $this->ajaxReturn($data);
    }
    /**
     * 道具添加
     */
    public function propOption(){
        $data = I();
        $data['admin'] = session('admin');
        $data['updatetime'] = time();
        $icon = $this->uploadImg();
        $data['icon'] = implode(" ",$icon['icon']);
        // $data['id'] = (new  \Org\Twitter\IdWork(31,31))->nextId();
        $data['createtime']  = time();
        if(M('item')->add($data)){
            $this->ajaxReturn(array('success' => 1, 'msg' => '用户新增成功'));
        }else{
            $this->ajaxReturn(array('success' => 0, 'msg' => '用户增加失败'));
        }
    }

    /**
     * 删除道具信息
     */
    public function delProp(){
        $itemid = I('itemid');
        if(M('item')->where("itemid='$itemid'")->delete()){
            $result = array(
                'sta' => 1, 
                'msg' => '删除成功'
            );
        }else{
            $result = array(
                'sta' => 0, 
                'msg' => '删除失败'
            ); 
        }
        $this->ajaxReturn($result);
    }

    /**
    * 查看daoju详情
    * @author wang
    * @param 
    */
    public function itemDetial(){
        $itemid = I('itemid');
        $data = M('item as i')
                ->join('LEFT JOIN '.C('DB_PREFIX').'game as g ON i.gameid = g.gameid')
                ->join('LEFT JOIN '.C('DB_PREFIX').'currency as c ON i.currencyid = c.curyid')
                ->field('i.*,g.game,c.currency')
                ->where("itemid='$itemid'")
                ->find(); 
        $data['orderid']=(int)$data['orderid'];
        $data['createtime']=date("Y-m-d H:i:s",$data['createtime']);
        $data['updatetime']=date("Y-m-d H:i:s",$data['updatetime']);
        $game_id = M('game')->select();
        $currency_id = M('currency')->select();
        $this->assign("game_id",$game_id);
        $this->assign("currency_id",$currency_id);
        $this->assign("data",$data);
        $this->display('detial');
    }


    /**
    * 修改内容
    * @author wang
    * @param 
    */
    public function editImg(){
      $data = I();
      $data['admin'] = session('admin');
      $data['updatetime'] = time();
      $itemid = $data['itemid'];
      $icon = $this->uploadImg();
      if($icon){
         $data['icon'] = implode(" ",$icon['icon']); 
      }
      if(M('item')->where("itemid='$itemid'")->save($data)){
          $this->ajaxReturn(1);
      }else{
          $this->ajaxReturn(0);
      }
    }


}

